package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.api.oa.mapper.YQTrackMapper;
import com.api.oa.model.entity.YQTrack;
import com.api.oa.model.vo.OaOrgTreeVO;
import com.api.oa.services.YQTrackService;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class YQTrackServiceImpl implements YQTrackService {

    @Autowired
    private YQTrackMapper mapper;


    @Override
    public Integer add(YQTrack entity) {
        entity.setTimestamp(DateUtil.date().toTimestamp());
        return mapper.insert(entity);
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public Integer update(YQTrack entity) {
        return mapper.update(entity);
    }

    @Override
    public PageInfo<YQTrack> list(Long pid, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("createdAt desc,trackId desc");
        return new PageInfo<>(mapper.list(pid));
    }

    @Value("${baidu.key}")
    private String baiduKey;

    @Override
    public List<YQTrack> all(Long taskId) {
        List<YQTrack> all = mapper.all(taskId);
        Map<String, Object> params = new HashMap<>();
        params.put("ak", baiduKey);
        //params.put("rectify_option", "need_mapmatch:1|transport_mode:walking|denoise_grade:1|vacuate_grade:0");
        List<Map<String, Object>> collect = all.stream().map(i -> {
            Map<String, Object> map = new HashMap<>();
            map.put("loc_time", i.getTimestamp().getTime() / 1000);
            map.put("latitude", i.getLat());
            map.put("longitude", i.getLng());
            map.put("coord_type_input", "wgs84");
            return map;
        }).collect(Collectors.toList());
        params.put("point_list",JSONUtil.toJsonStr(collect));
        String post = HttpUtil.post("https://api.map.baidu.com/rectify/v1/track", params);
        JSONObject jsonObject1 = JSONUtil.parseObj(post);
        List<YQTrack> tracks = new ArrayList<>();
        if (jsonObject1.getInt("status") == 0) {
            JSONArray points = jsonObject1.getJSONArray("points");
            for (int i = 0; i < points.size(); i++) {
                JSONObject jsonObject2 = points.getJSONObject(i);
                YQTrack yqTrack = new YQTrack();
                yqTrack.setLat(jsonObject2.getDouble("latitude"));
                yqTrack.setLng(jsonObject2.getDouble("longitude"));
                yqTrack.setTimestamp(DateUtil.date(jsonObject2.getLong("loc_time")));
                tracks.add(yqTrack);
            }
        }
        return tracks;
    }

    @Override
    public YQTrack one(Long id) {
        return mapper.findOne(id);
    }


}
